<?php
/**
-------------------------
GNU GPL COPYRIGHT NOTICES
-------------------------
This file is part of FlexicaCMS.

FlexicaCMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlexicaCMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlexicaCMS.  If not, see <http://www.gnu.org/licenses/>.*/

/**
 * $Id$
 *
 * @author FlexicaCMS team <contact@flexicacms.com>
 * @link http://www.flexicacms.com/
 * @copyright Copyright &copy; 2009-2010 Gia Han Online Solutions Ltd.
 * @license http://www.flexicacms.com/license.html
 */
  
class FileService extends FServiceBase
{
    public function cmdDownloadWidgetFile($params)
    {
        $wid = $this->getParam($params, 'wid', 0);
        $fid = $this->getParam($params, 'fid', '');
        
        if (!$wid)
            return $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Invalid WID.'));
        elseif (empty($fid))
            return $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Invalid FID.'));
            
        //find instance
        $result = FSM::_run('Cms.WidgetData.get', array('id'=>$wid));
        if ($result->hasErrors()) {
            Yii::log(CVarDumper::dumpAsString($result->getErrors()), CLogger::LEVEL_ERROR);
            return $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Invalid WID.'));
        }
        
        $model = $result->model;
        eval('$data = '.$model->data.';');
        
        if (isset($data) && is_array($data))
        {
            $position = false;
            if (preg_match("/([a-z0-9_\-]+)\[(\d+)\]/i", $fid, $matches))
            {
                $fid = $matches[1];
                $position = (int) $matches[2];
                $position--;
            }
            if ($position !== false)
            {
                //multi-file
                if (isset($data[$fid.'_file']) && isset($data[$fid.'_file'][$position]))
                {
                    $file = $data[$fid.'_file'][$position];
                    $file = Yii::getPathOfAlias('application.*').'/../'.ltrim($file, '/');
                    if (file_exists($file))
                        Yii::app()->request->sendFile(basename($file), file_get_contents($file));
                    else
                        $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','File not found.'));
                }
                else
                    $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Invalid FID.'));
            }
            else
            {
                if (isset($data[$fid.'_file']))
                {
                    $file = $data[$fid.'_file'];
                    $file = Yii::getPathOfAlias('application.*').'/../'.ltrim($file, '/');
                    if (file_exists($file))
                        Yii::app()->request->sendFile(basename($file), file_get_contents($file));
                    else
                        $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','File not found.'));
                }
                else
                    $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Invalid FID.'));
            }
        }
        else
            $this->result->fail(ERROR_INVALID_DATA, Yii::t('Cms.File','Data stored error.'));
        
        return $this->result;
    }
}
